home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group00a.txt
/
000013_icon-group-sender _Thu Jan 20 17:03:40 2000.msg
< prev
next >
Wrap
Internet Message Format
|
2001-01-03
|
2KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id RAA23617
for icon-group-addresses; Thu, 20 Jan 2000 17:02:03 -0700 (MST)
Message-Id: <200001210002.RAA23617@baskerville.CS.Arizona.EDU>
Date: Thu, 20 Jan 2000 12:02:05 -0800 (PST)
From: Shamim Mohamed <soar@drones.com>
To: icon-group@optima.CS.Arizona.EDU
Subject: Re: all permutations
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
> I need a program which generates all combinations (or permutations?)
> of 12345, i.e. 12345, 12354, 12435, etc
I wrote a program to do this some time ago. It actually writes out the
permutations lexicographically, and is non-recursive. I don't remember
everything about it, so if something seems strange, I might not be able
to explain it!
-s
# procedure to find the next (lexicographically) permutation of a string
# Algorithm : to generate one string from the previous,
# i) scan left from the end of the string till you find a char smaller
# than the one to its right;
# ii) find the smallest letter in the substring to its right; interchange
# them;
# iii) sort the substring on the right
# and repeat.
# This is a permutation since letters are only interchanged. This is
# the next higher one by inspection. (Very long and careful inspection!)
procedure next_perm(s)
i := *s - 1
while i > 0 & ord(s[i]) >= ord(s[i+1]) do
i -:= 1
if i = 0 then fail
j := i
min := i + 1
while j <= *s do {
if ord(s[j]) > ord(s[i]) & ord(s[j]) < ord(s[min]) then
min := j
j +:= 1
}
s[min] :=: s[i]
s[i+1:0] := ssort(s[i+1:0])
return s
end
procedure main(args)
s := args[1] | "abcde"
write(i:=1, " ", s)
while s := next_perm(s) do
write(i+:=1, " ", s)
end
procedure ssort(s)
# The straightforward approach doesn't work if the string contains duplicates
return string(cset(s))
ret := ""
T := table(0)
every c := !s do T[c] +:= 1
every c := !cset(s) do
ret ||:= repl(c, T[c])
return ret
end